package org.aman.publish.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @author DaiJG
 * @title RunThread功能描述
 * @Date created at 2019/8/30 11:30
 * @Description
 * @since 1.0.0
 */
public class CmdRespThread extends Thread{
    InputStream is;
    String printType;
    List<String> result;
    
    public static final String INFO = "INFO";
    public static final String ERROR = "ERROR";
    
    CmdRespThread(InputStream is, String printType, List<String> result) {
        this.is = is;
        this.printType = printType;
        this.result = result;
    }

    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null){
                if(INFO.equals(printType)){
                    System.out.println(line);
                } else {
                    System.err.println(line);
                }
                if(result != null){
                    result.add(printType + " > " + line);
                }
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
